package com.bkgy.modular.rawMaterialRules.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.rawMaterialRules.entity.Calthreshold;
import com.bkgy.modular.rawMaterialRules.mapper.CalthresholdMapper;
import com.bkgy.modular.rawMaterialRules.service.CalthresholdService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CalthresholdServiceImpl extends ServiceImpl<CalthresholdMapper,Calthreshold> implements CalthresholdService {
    @Resource
    private CalthresholdMapper calthresholdMapper;
    /**
     * 2130酸轧成品连退原料阈值数据查询接口
     * @return
     */
    @Override
    public ResultData findCalthresholdData() {
        List<Calthreshold> list = calthresholdMapper.selectList(null);
        return ResultData.ResultDataFaultSelf("查询成功",list);
    }

    /**
     * 2130酸轧成品连退原料阈值更新数据
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData updateCalthresholdData(JSONObject jsonObject) {
        try {
            // 获取前端传进的数据
            String dataId = jsonObject.getString("dataId");

            // 构建查询条件
            LambdaQueryWrapper<Calthreshold> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Calthreshold::getGrade1,dataId);

            // 获取传进需要修改的数据,并转成String类型
            String data = jsonObject.getJSONObject("data").toJSONString();

            // 调用json中的parseObject,将字符串data转成java对象
            Calthreshold calthreshold = JSON.parseObject(data, Calthreshold.class);

            // 调用mapper进行修改
            calthresholdMapper.update(calthreshold,queryWrapper);
            return ResultData.ResultDataFaultSelf("更新成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败，错误信息为" + e.getMessage(), null);
        }
    }
}
